package com.controller.secretary;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.model.DbFun;
import com.model.Stu_chose_Tch;
import com.model.Student;
import com.model.Teacher;
import com.model.Thesisprogressrecord;

public class setSecretaryController extends Controller{
	
	public void index()
	{
		
	}
	
	/**
	 * 综合设置表格
	 */
	public void setTable()
	{
		int secId = getSessionAttr("teacher_id");
		System.out.println(getSessionAttr("teaId"));
//		Integer secId=209;
		Integer depId=Teacher.dao.searchDepId(secId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.departmentMessage(page, rows, depId));
	}
	
	/**
	 * 综合设置
	 */
	public void setMessage()
	{
		Integer tch_stu_num=getParaToInt("department_tch_stu_num");
		Integer progress_month=getParaToInt("deparment_progress_month");
		Integer pass_percent=getParaToInt("department_pass_percent");
		Integer secId=209;
		Integer depId=Teacher.dao.searchDepId(secId);
		boolean success=false;
		success = DbFun.dao.setMessage(depId, tch_stu_num, progress_month, pass_percent);
		renderJson("{\"success\":"+success+"}");
	}
	
	/**
	 * 领导小组表格
	 */
	public void groupTable()
	{
		int secId = getSessionAttr("teacher_id");
//		Integer secId=209;
		Integer depId=Teacher.dao.searchDepId(secId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.steeringGroup(page, rows, depId));
	}
	
	/**
	 * 系领导小组下拉框（未分配）
	 */
	public void groupTeacherList()
	{

		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		renderJson(DbFun.dao.groupTeacherList(depId));
	}
	
	/**
	 * 设置领导小组
	 * @throws UnsupportedEncodingException 
	 */
	public void setGroup() throws UnsupportedEncodingException
	{
//		int secId=getParaToInt();
		int tchId=getParaToInt("tchId");
		String category = new String(getPara("category").getBytes("iso8859-1"),"utf-8");
		renderJson(Teacher.dao.setGroup(tchId, category));
	}
	
	/**
	 * 删除领导小组成员
	 */
	public void deleteGroup()
	{
		boolean success=false;
//		int secId=getParaToInt();
		int tchId=getParaToInt("tchId");
		success = Teacher.dao.deleteGroup(tchId);
		renderJson("{\"success\":"+success+"}");
	}
	
	/**
	 * 分配结果表格(所有教师)
	 */
	public void result_depId()
	{
		int secId = getSessionAttr("teacher_id");
//		Integer secId=209;
		Integer depId=Teacher.dao.searchDepId(secId);
		System.out.println("depId:"+depId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.resultTable_depId(page, rows, depId));
	}
	
	/**
	 * 分配结果表格(未分配教师)
	 */
	public void result_no()
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.resultTable_no(page, rows, depId));
	}
	
	/**
	 * 分配结果表格(查询某老师)
	 */
	public void result_teacher() 
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int tchId=getParaToInt("tchId");
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.resultTable_tchId(page, rows, tchId));
	}
	
	/**
	 * 读取Excel表格，建立关联
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public void importExcel() throws FileNotFoundException, IOException
	{
		UploadFile uf=getFile();
		String path = uf.getSaveDirectory();
		String name = uf.getFileName();
//		int num = Integer.parseInt(name.substring(0, 2));
		System.out.println(path+name);
		File File = new File(path+name);
		System.out.println("isFile:"+File.isFile());
		inputExcel_stuTch(path+name);
		uf.getFile().delete();
		renderJson("上传分配成功");
	}
	
	
	/**
	 * 读取EXCEL建立学生指导老师关联
	 * @param path
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void inputExcel_stuTch(String path) throws FileNotFoundException, IOException 
	{	
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(path));
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheetAt(0);
		HSSFRow row;

		boolean success=false;
		int count = sheet.getPhysicalNumberOfRows();
		System.out.println("count:"+count);
		for(int i=1;i<count;i++)
		{
			System.out.println("第："+i+"行");
			row = sheet.getRow(i);
			Map<String, String> temp = new HashMap<String, String>();
			String stuNum=row.getCell(0).toString().trim();
			String stuName=row.getCell(1).toString().trim();
			Integer stuId = Student.dao.searchStuId(stuName, stuNum);
			String tchNum=row.getCell(2).toString().trim();
			String tchName=row.getCell(3).toString().trim();
			Integer tchId=Teacher.dao.searchTchId(tchName, tchNum);
			System.out.println(stuName+"-"+stuNum+"-"+tchNum+"-"+tchName);
			System.out.println(stuId+"-"+tchId);
			if(stuId!=null&&tchId!=null)
			{
				if(DbFun.dao.checkRela_stu_tch(stuId))
				{
					success=DbFun.dao.createTch_rela_stu(stuId, tchId);
					System.out.println("success:"+success);
				}
				else
					continue;
			}
			else
				continue;
		}
	}
	
	/**
	 * 查询该系的教师列表
	 */
	public void teacherList()
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		renderJson(DbFun.dao.teacherList(depId));
	}
	
	/**
	 * 查询该系的未分配完毕教师列表
	 */
	public void teacherList_no()
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		renderJson(DbFun.dao.teacherList_no(depId));
	}
	
	/**
	 * 分配学生表格
	 */
	public void studentList()
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		Map<String, Object> m = new HashMap<String, Object>();
		m.put("total", Stu_chose_Tch.dao.chose_stu(page, rows, depId).getTotalRow());
		m.put("rows", Stu_chose_Tch.dao.chose_stu(page, rows, depId).getList());
		renderJson(m);
	}
	
	/**
	 * 查询老师剩余名额
	 */
	public void oddAmount()
	{
		int tchId = getParaToInt("tchId");
		Double amount = DbFun.dao.oddAmount(tchId);
		renderJson(amount);
	}
	
	/**
	 * 分配学生
	 */
	public void allotStudent()
	{
//		List<Record> studentList = getPara();
		int count;
		String stuidList=getPara("stuId_List");
		String[] stuList=stuidList.split(",");
		Integer tchId=getParaToInt("tchId");
		Double obbAmount=DbFun.dao.oddAmount(tchId);
		Map<String, Object> m = new HashMap<String, Object>();
		for(count=0;count<stuList.length && count<obbAmount;count++)
		{
			if(DbFun.dao.checkRela_stu_tch(Integer.parseInt(stuList[count])))
				DbFun.dao.createTch_rela_stu(Integer.parseInt(stuList[count]), tchId);
			else
				continue;
		}
		m.put("msg1", "分配学生"+count+"个");
		if((obbAmount-count)>=0)
		{
			m.put("msg2", "教师剩余名额"+(obbAmount-count)+"个");
		}
		else
		{
			m.put("msg2", "教师剩余名额0个");
		}
		renderJson(m);
	}
	
	/**
	 * 指导教师与评阅教师分配结果表格
	 */
	public void guideRelajudgeTable()
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int depId=Teacher.dao.searchDepId(secId);
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.guideRelajudge(page, rows, depId));
	}
	
	/**
	 * 评阅教师分配结果表格(查询某老师)
	 */
	public void guideRelajudge_teacher() 
	{
		int secId = getSessionAttr("teacher_id");
//		int secId=209;
		int tchId=getParaToInt("tchId");
		Integer page = getParaToInt("page");
		Integer rows = getParaToInt("rows");
		renderJson(DbFun.dao.guideRelajudge_tchId(page, rows, tchId));
	}
	
	/**
	 * 读取Excel表格，建立指导老师评阅老师关联
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public void importExcel_gTch_jTch() throws FileNotFoundException, IOException
	{
		UploadFile uf=getFile();
		String path = uf.getSaveDirectory();
		String name = uf.getFileName();
//		int num = Integer.parseInt(name.substring(0, 2));
		System.out.println(path+name);
		File File = new File(path+name);
		System.out.println("isFile:"+File.isFile());
		inputExcel_gTch_jTch(path+name);
		uf.getFile().delete();
		renderJson("上传分配成功");
	}
	
	/**
	 * 读取Excel建立指导教师与评阅教师关联
	 * @param path
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public void inputExcel_gTch_jTch(String path) throws FileNotFoundException, IOException 
	{	
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(path));
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		HSSFSheet sheet = wb.getSheetAt(0);
		HSSFRow row;

		boolean success=false;
		int count = sheet.getPhysicalNumberOfRows();
		System.out.println("count:"+count);
		for(int i=1;i<count;i++)
		{
			System.out.println("第："+i+"行");
			row = sheet.getRow(i);
			String guideNum=row.getCell(0).toString().trim();
			String guideName=row.getCell(1).toString().trim();
			System.out.println(guideNum+"-"+guideName);
			Integer guideId = Teacher.dao.searchTchId(guideName, guideNum);
			String judgeNum=row.getCell(2).toString().trim();
			String judgeName=row.getCell(3).toString().trim();
			System.out.println(judgeNum+"-"+judgeName);
			Integer judgeId=Teacher.dao.searchTchId(judgeName, judgeNum);
			System.out.println(guideNum+"-"+guideName+"-"+judgeNum+"-"+judgeName);
			System.out.println(guideId+"-"+judgeId);
			if(guideId!=null&&judgeId!=null)
			{
				if(DbFun.dao.checkRela_guide_judge(guideId))
				{
					success=DbFun.dao.create_guideRelajudge(guideId, judgeId);
					System.out.println("success:"+success);
				}
				else
					continue;
			}
			else
				continue;
		}
	}
	
	/**
	 * 自动分配指导教师与评阅教师
	 */
	public void autoAllot_gTch_jTch()
	{
		int index=0;
		String sql="SELECT teacher_id from guide_rela_judge_msg where teacher_id1 IS NULL";
		List<Integer> tchIdList= Db.query(sql);
		System.out.println("list.size1:"+tchIdList.size());
		int count=tchIdList.size();
		if((tchIdList.size()%2) == 1)
		{
			if(DbFun.dao.checkRela_guide_judge(tchIdList.get(tchIdList.size()-1)))
				DbFun.dao.create_guideRelajudge(tchIdList.get(tchIdList.size()-1),tchIdList.get(0) );
			tchIdList.remove(tchIdList.get(tchIdList.size()-1));
			count--;
		}
		System.out.println("list.size1:"+tchIdList.size());
		for(index = 0;index<tchIdList.size();index=index+2)
		{
			System.out.println("index:"+index);
			System.out.println("tch1:"+tchIdList.get(index));
			System.out.println("tch2:"+tchIdList.get(index+1));
			if(DbFun.dao.checkRela_guide_judge(tchIdList.get(index)))
				DbFun.dao.create_guideRelajudge(tchIdList.get(index), tchIdList.get(index+1));
			if(DbFun.dao.checkRela_guide_judge(tchIdList.get(index+1)))
				DbFun.dao.create_guideRelajudge(tchIdList.get(index+1), tchIdList.get(index));
		}
		if(count==index)
			renderJson("{\"success\":true}");
		else
			renderJson("{\"success\":false}");
	}
}
